﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Windows;

namespace Xceed.Silverlight.DataGrid
{
  internal class CellContentElement
  {
    #region Constructors

    internal CellContentElement( FrameworkElement container, Type dataType )
    {
      if( container == null )
        throw new ArgumentNullException( "container" );

      if( dataType == null )
        throw new ArgumentNullException( "dataType" );

      m_container = container;
      m_dataType = dataType;
    }

    internal CellContentElement( DataTemplate template, Type dataType )
    {
      if( template == null )
        throw new ArgumentNullException( "template" );

      if( dataType == null )
        throw new ArgumentNullException( "dataType" );

      m_container = template.LoadContent() as FrameworkElement;
      m_template = template;
      m_dataType = dataType;

      if( m_container == null )
        throw new ArgumentException( "The root of the template must be a FrameworkElement.", "template" );
    }

    #endregion

    #region Container Property

    internal FrameworkElement Container
    {
      get
      {
        return m_container;
      }
    }

    private readonly FrameworkElement m_container;

    #endregion

    #region Template Property

    internal DataTemplate Template
    {
      get
      {
        return m_template;
      }
    }

    private readonly DataTemplate m_template;

    #endregion

    #region DataType Property

    internal Type DataType
    {
      get
      {
        return m_dataType;
      }
    }

    private readonly Type m_dataType;

    #endregion

    public override int GetHashCode()
    {
      return m_container.GetHashCode();
    }

    public override bool Equals( object obj )
    {
      CellContentElement item = obj as CellContentElement;
      if( item == null )
        return false;

      if( item == this )
        return true;

      if( item.GetHashCode() != this.GetHashCode() )
        return false;

      return ( item.Container == this.Container )
          && ( item.Template == this.Template )
          && ( item.DataType == this.DataType );
    }
  }
}
